package com.huaishushu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huaishushu.common.CustomException;
import com.huaishushu.common.R;
import com.huaishushu.entity.Category;
import com.huaishushu.entity.Dish;
import com.huaishushu.entity.Employee;
import com.huaishushu.entity.Setmeal;
import com.huaishushu.mapper.CategoryMapper;
import com.huaishushu.service.ICategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sun.jdi.event.StepEvent;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

/**
 * <p>
 * 菜品及套餐分类 服务实现类
 * </p>
 *
 * @author ChiBaoBaoHuaiShuShu
 * @since 2023-05-28
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements ICategoryService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private DishServiceImpl dishService;

    @Autowired
    private SetmealServiceImpl setmealService;

    /**
     * 新增分类和套餐
     * @param category
     * @return
     */
    @Override
    public boolean save(Category category) {
        return categoryMapper.insert(category) > 0;
    }

    /**
     * 分类信息的分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public Page<Category> page(Integer page, Integer pageSize) {
        //构建分页构造器
        Page pageInfo = new Page(page, pageSize);
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(Category::getUpdateTime);
        //此处不返回是因为查询好后的值是直接赋值给了上面的分页构造器了pageInfo
        categoryMapper.selectPage(pageInfo, lqw);
        return pageInfo;
    }

    /**
     * 根据id来删除分类信息
     * 删除之前判断菜品是否绑定了分类，没绑定分类信息则删，绑定了的话不能删除
     * @param id
     * @return
     */
    @Override
    public boolean delete(Long id) {
        //查询当前分类是否关联了菜品，如果已经关联，抛出一个业务异常
        LambdaQueryWrapper<Dish>dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        long count = dishService.count(dishLambdaQueryWrapper);
        if (count > 0) {
            // 抛出一个业务异常
            throw new CustomException("当前分类关联了菜品，不能删除！！！");
        }
        //查询当前分类是否关联了套餐，如果已经管理了抛出一个业务异常
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        count = setmealService.count(setmealLambdaQueryWrapper);
        if (count > 0) {
            // 抛出一个业务异常
            throw new CustomException("当前分类关联了套餐，不能删除！！！");
        }
        //正常删除分类
        return categoryMapper.deleteById(id)>0;
    }

    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    @Override
    public boolean update(Category category) {
        return categoryMapper.updateById(category) > 0;
    }

    /**
     * 根据条件来查询所有分类信息
     * @param category
     * @return
     */
    @Override
    public List<Category> list(Category category) {
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        lqw.eq(category.getType() != null, Category::getType, category.getType());
        //添加个排序，一个是按顺序升序排序，一个是按更新时间降序排序
        lqw.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        List<Category> categoryList = categoryMapper.selectList(lqw);
        return categoryList;
    }


}
